""" Input mode deferrer. """

import plotCourse, dragMap, showBoat

from pgEngine.boat import Boat
from pgEngine.point import Point
import pgEngine.game as game

import pygame
from pygame.locals import *

class MapMode(object):
    def __init__(self, game, window):
        self.game = game
        self.window = window
        self.res = window.map.res

        # Make sure we draw the highlight.
        self.reAdd = False
        
        # Load the cheezy compass.  Wind direction.
        self.compass = self.res.loadImage("compass.png")
        self.windArrow = self.res.loadImage("windArrow.png")
        self.wind = {}
    
    def onEvent(self, event):
        retVal = None
        
        if (event.type == MOUSEBUTTONDOWN and
            self.window.checkOverlayClick(event.pos)):
            
            if event.button == 1:
                mapPos = self.window.map.getMousePos()
                items = self.game.ocean.getItems(mapPos[0]-1,
                                                 mapPos[1]-1,
                                                 3, 3)
                for loc, item in items:
                    if isinstance(item, Boat):
                        if not item.anchored and self.game.state in \
                            (game.PirateGame.WAITING_PLAYERS_TURNS, 
                             game.PirateGame.WAITING_MY_TURN):
                            # Start plotting a course for a ship.
                            newMode = plotCourse.PlotCourseMode(self.game,
                                                                self.window,
                                                                item)
                            self.window.mode.append(newMode)
                            return False
                        elif self.game.state == game.PirateGame.EXECUTING_ATTACKS:
                            #TODO: Select a crewman, and start attacking.
                            pass
                    
                # Dragging the map.
                if self.window.mode[-1] == self:
                    newMode = dragMap.DragMapMode(self.window)
                    self.window.mode.append(newMode)                    
                    retVal = False
                
            elif event.button == 4:
                self.window.mappos.y += 4
                retVal = False
            
            elif event.button == 5:
                self.window.mappos.y -= 4
                retVal = False
            
            elif event.button == 6:
                self.window.mappos.x += 4
                retVal = False
            
            elif event.button == 7:
                self.window.mappos.x -= 4
                retVal = False

        elif (event.type == MOUSEMOTION
              and self.window.checkOverlayClick(event.pos)):

            mapPos = self.window.map.getMousePos()
            items = self.game.ocean.getItems(mapPos[0]-1.5,
                                             mapPos[1]-1.5,
                                             3.0, 3.0)
            for loc, item in items:
                if isinstance(item, Boat):
                    # Start plotting a course for a ship.
                    newMode = showBoat.ShowBoatMode(self.game,
                                                    self.window,
                                                    item)
                    self.window.mode.append(newMode)
                    return False
        
        return retVal
        
    def drawMenu(self):
        wind = (self.game.weather.dir.dir, self.game.weather.distance)
        if wind not in self.wind:
            self.wind[wind] = pygame.transform.rotozoom(self.windArrow,
                                                        wind[0] * 45.0,
                                                        wind[1] / 1.5)
        rcStatus = self.window.overlayRects["status"]
        ptCompass = Point(*rcStatus.bottomleft)
        ptCompass -= Point(*self.compass.get_size()) * 0.5
        # Center it in the status area
        # And move it up from the bottom
        ptCompass += Point(rcStatus.width, -100) * 0.5
        
        self.window.screen.blit(self.compass, ptCompass.pos)
        
        rcCompass = pygame.Rect(ptCompass.pos, self.compass.get_size())
        rcArrow = pygame.Rect((0,0), self.wind[wind].get_size())
        rcArrow.center = rcCompass.center
        self.window.screen.blit(self.wind[wind], rcArrow)
        

    def drawMap(self):
        self.window.map.draw(self.window.curT,
                             self.game.ocean,
                             self.window.mappos)


    def onDrawMapObjects(self, surface):
        """ Draw the highlight if we're the current mode. """

        if self.game.state == self.game.STOPPED:
            self.game.start()
                
        
        if self.window.mode[-1] != self:
            return
        
        wndMap = self.window.map
        mousePos = (self.window.mouseX, self.window.mouseY)
        if not self.window.checkOverlayClick(mousePos):
            pass
        if self.window.checkOverlayClick(mousePos):
            ptHighlight = (Point(*mousePos) -
                           Point(*wndMap.rcMap.topleft) -
                           (wndMap.gridSize/2))
            surface.blit(wndMap.highlight, ptHighlight.pos)

